package logic

import (
	"class/cache"
	"context"
	"math/rand"
	"time"

	"class/classrpc/classrpc"
	"class/classrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 短信发送
func (l *SendSmsLogic) SendSms(in *classrpc.SendSmsRequest) (*classrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line

	code := rand.Intn(9000) + 1000

	//i, _ := cache.Rdb.Get(l.ctx, "sendSms"+in.Account).Int64()
	//if i >= 1 {
	//	return nil, fmt.Errorf("当前已经发送过一次短信")
	//}
	//
	//sms, err := pkg.SendSms(in.Account, strconv.Itoa(code))
	//if err != nil {
	//	return nil, fmt.Errorf(sms.Msg)
	//}

	cache.Rdb.Set(l.ctx, "sendSms"+in.Account, code, time.Minute*5)

	return &classrpc.SendSmsResponse{}, nil
}
